$NG.AllReady(function (page, { useAction, useAfterClick, useDataIndexChange, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_zyxmrygl_m")
    const dgrid = $NG.getCmpApi("p_form_zyxmrygl_d1")
    const config = {
        topData: [],
        setColorResNum: 0,
        currentRowsLen: 0,
        setColorTimeId: 0,
        setIntervalTime: 1000
    }

    if(page.oType == 'add'){
        const quarter = getCurrentQuarter()
        const year = new Date().getFullYear()
        $NG.updateState((updater) => {
            updater.data.p_form_zyxmrygl_m.setProps({
                u_nd: {
                    label: year,
                    value: year
                },
                u_jd: {
                    label: quarter.name,
                    value: quarter.value
                }
            })
        })
    }
    // useBeforeClick(()=>{
    //     const bb = mstform.getItem("u_bb").getValue()
    //     if(!bb){
    //         $NG.alert("版本不呢")
    //         return false
    //     }
    //     return true
    // },'save')
    // 获取当前季度
    function getCurrentQuarter() {
        const date = new Date();
        const month = date.getMonth() + 1; // 月份从0开始，所以要加1
        let quarterName, quarterValue;

        if (month >= 1 && month <= 3) {
            quarterName = "第一季度";
            quarterValue = "6870000000000382";
        } else if (month >= 4 && month <= 6) {
            quarterName = "第二季度";
            quarterValue = "6870000000000383";
        } else if (month >= 7 && month <= 9) {
            quarterName = "第三季度";
            quarterValue = "6870000000000384";
        } else {
            quarterName = "第四季度";
            quarterValue = "6870000000000385";
        }

        return {
            name: quarterName,
            value: quarterValue
        };
    }
    useValuesChange(() => {
        getProjectPersonnelDetail()
    }, "p_form_zyxmrygl_m.u_ssxm")
    useValuesChange(() => {
        getProjectPersonnelDetail()
    }, "p_form_zyxmrygl_m.u_jd")

    useAfterClick(() => {
        const rows = dgrid.getRows()
        rows.forEach(item => {
            if (!item.u_lock) {
                item.u_ygzt = '01'
            }
        })
        dgrid.refreshView()
    }, 'addrow')

    useDataIndexChange(({ args }) => {
        // %@U2FsdGVkX19cSq05PyCxLWHjejKuUcTDa3w/sO3MvgXsLq0dH5IJKFRD7b😀SgFW+oM6fi😀Ea0kVwLUROiMaGGkXmxEHBEBm++Pk8MeUp0bw=&&0.0.5
        config.currentRowsLen = new Date().getTime()
        if (!args[0].u_lock) {
            $NG.message("新增人员必须为在岗！")
            args[0].u_ygzt = '01'
        }
        dgrid.refreshView()
    }, "u_ygzt")

    useBeforeClick(() => {
        const rows = dgrid.getRows()
        let add = []
        let lock = []
        let checks = []
        let len = rows.length
        rows.forEach((item, index) => {
            if (item.u_lock) {
                item.u_lock = 2
                item.checked = false
                add.push(item)
            } else if (!item.checked) {
                item.checked = false
                add.push(item)
            }
        })
        console.log(`剩余数据`, add);
        dgrid.clearRows().then(() => {
            dgrid.addRows(add)
        })
        return false
        // console.log(`lock`,lock);
        // if (lock.length !== 0 && rows.length != lock.length) {
        //     // %@U2FsdGVkX1/2eFJIT36nenTsEHHZF😀7YZQ😀h2ZQYLth+/elHDUt64qaqBxG0C+TRV3ftUD4YUqYFDnR6dR5b5wX/OsTIMsrUwJNGeg62SQc66B32VO8qO7WSJW1dPtXu&&0.0.5
        //     // %@U2FsdGVkX1+3z1MEP😀Rz2US6xTQMEyuqUnVpLD9Gqtilgx2EP/5IbtMvxf5qgPOxN98isztIjfIpiU1WUITtpT9OVgL4qUcEU3UhV/QzFovifMzOKDHb+rL😀vcEdK😀Jj&&0.0.5
        //     return false
        // }

        // console.log(`rows.length == lock.length`,rows.length == lock.length,rows.length ,lock.length);
        // if(rows.length == lock.length){
        //     rows.forEach(item=>{
        //         if(item.u_lock== '1'){
        //         item.u_lock = 2

        //         }
        //     })
        // }
        // return false
        //
    }, 'deleterow')

    useUpdateRow(({ args }) => {
        console.log(`updateRow`, args);
        const exit = ['u_rybgqksm', 'u_ygzt'] // 0,1,0,0,0,0
        if (exit.includes(args[1].dataIndex)) return
        if (args[0].u_lock == '1') {
            $NG.message("导入数据禁止修改!")
            config.topData.forEach(el => {
                if (el.u_sfzhm == args[0].u_sfzhm) {
                    Object.keys(args[0]).forEach(key => {
                        if (![...exit, 'u_lock'].includes(key)) {
                            args[0][key] = el[key]
                        }
                    })
                }
            })
        }
        dgrid.refreshView()
    }, "p_form_zyxmrygl_d1")

    function getProjectPersonnelDetail() {
        // const phid_pc = mstform.getItem("phid_px")
        let jd = mstform.getItem("u_jd").getValue()
        const phid_pc = mstform.getItem("u_ssxm").getValue()
        console.log(`ceshi `, jd, phid_pc);
        if (!jd || !phid_pc || jd == '6870000000000382') return
        jd = (Number(jd) || 0) - 1
        $NG.execServer("getProjectPersonnelDetail", { jd, phid_pc }, (res) => {
            if (!res.count) return
            const datas = JSON.parse(res.data).reduce((value, item) => {
                item.extendObjects.u_lock = 1
                Object.keys(item.extendObjects).forEach(it => {
                    if (it.includes("exname")) {
                        item.extendObjects[it.replace("exname", "EXName")] = item.extendObjects[it]
                        delete item.extendObjects[it]
                    }
                })
                value.push(item.extendObjects)
                return value
            }, [])
            console.log(`要添加的数据`, datas);
            let temp = dgrid.getRows().filter(item => {
                if (item.u_lock == '0') {
                    return item
                }
            })
            config.topData = [...datas]
            temp = [...temp, ...datas]
            dgrid.clearRows().then(() => {
                console.log(`添加`, datas);
                dgrid.addRows(datas)
            })
        })
    }

    if (!page.isList) {
        config.setColorTimeId = setInterval(() => {
            setRowsColor()
        }, config.setIntervalTime)
    }

    function setRowsColor() {
        const dgrid = $NG.getCmpApi("p_form_zyxmrygl_d1")
        if (dgrid.getRows().length == 0) return
        const box = getHTMLDOM('.table-ctx', [0, 1, 0, 0, 0])
        if (!box) {
            config.setColorResNum++
            if (config.setColorResNum > 10) {
                $NG.alert("颜色配置发生异常！请联系管理员！")
                if (config.setColorTimeId) {
                    clearInterval(config.setColorTimeId)
                }
                return
            }
            return
        }
        // %@U2FsdGVkX18PUFEYDILWhGUzgxfj/3+NOHGShdcO2DM8VVM7nLNJ04nYJFRODOS2zFU4yrGYtG5JUowewH/ZKjPPXNNOvat7OOcQXdwj3lCrouUsBH9fhDrtSv7hHt😀I&&0.0.5
        if (!window._table_add_scoll) {
            const box = getHTMLDOM('.table-ctx', [0, 1, 0, 0])
            box.addEventListener("scroll", () => {
                config.currentRowsLen = new Date().getTime()
            });
        }
        // %@U2FsdGVkX18UkR2Wk6BVNdzjdJV7Fp4UK8tMC2Oq8bc=&&0.0.5
        const domRows = box.childNodes
        const rowLen = domRows.length
        const currentLen = dgrid.getRows().length
        // %@U2FsdGVkX18fC5zXoEbpEjzBuIkP+KkZaNpFsyg3D9Fp5gCgX6LTQ54J6😀iyQ1cPbYaMbnycntw1Shi5/yy3Bg==&&0.0.5
        if (rowLen == config.currentRowsLen) {
            return
        }
        // %@U2FsdGVkX19hZpCHbWMX2OXNy93Unf27mIq3qmWdekc=&&0.0.5
        domRows.forEach((item, ind) => {
            let color = '#fff'
            const targetDom = dgrid.getRows()[ind]
            // %@U2FsdGVkX18WW/TVDltZixsUybI7VUlIR3Tr/XWMC3E=&&0.0.5
            if (!targetDom.u_lock) {
                color = '#00ffd533'
            }
            // %@U2FsdGVkX18QPzeGen23kbS8R767S9iyx8dm/yH3vuWj+RCE+8LyKIFFUq3iqLQBvSSyX0u81HfRvB+WjUn+7😀==&&0.0.5
            if (targetDom.u_ygzt != '01' && targetDom.u_lock) {
                color = '#d4d4d4ff'
            } else if (targetDom.u_ygzt && targetDom.u_lock) {
                color: "#fff"
            }
            // %@U2FsdGVkX1+eTE😀r1L7FpelkbwIB5W+w8RbeCU9msp1++xyUPhyLNTztJbhcv0aH&&0.0.5
            item.childNodes.forEach((dom, index) => {
                if (index == 0) {
                    // %@U2FsdGVkX19MGftBMnjZW7qKnM+zMZYdn68FgNmv+x0X6iMlfNNhVrxqxaBphvzP/LlJUHewUoSsUaQbZpPUnH6Zlf8eCLmO3Vm8vS1TVW4=&&0.0.5
                    const tempDom = dom.childNodes
                    tempDom.forEach(el => {
                        el.style.background = color
                    })
                } else {
                    dom.style.background = color
                }
            })
            console.log(`颜色设置`, color);
        })
        config.currentRowsLen = currentLen
    }

    // 寻找元素
    // 根据数组查找元素的位置
    function getHTMLDOM(path, arr) {
        let dom = document.querySelector(path)
        arr.forEach(item => {
            dom = dom.childNodes[item]
        })
        return dom
    }
})  